package com.Redis.Thread;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.Redis.Model.ReportData;
import org.apache.log4j.Logger;
public class StartTaskThread implements Runnable {
	private Logger logger = Logger.getLogger(this.getClass().getName());
	private ThreadPoolTaskExecutor threadPoolTaskExecutor;	
	private ReportData reportData;
	
	public StartTaskThread(ThreadPoolTaskExecutor threadPoolTaskExecutor,ReportData reportData) {
		this.threadPoolTaskExecutor = threadPoolTaskExecutor;
		this.reportData = reportData;
	}
	/**
	 * 执行相关任务
	 */
	@Override
	public synchronized void run() {		
		Thread thisThread = Thread.currentThread();	
		FutureTask<Boolean> futureTask = new FutureTask<Boolean>(new ThreadPoolTask(this.reportData));
		threadPoolTaskExecutor.execute(futureTask);
		// 在这里可以做别的任何事情  异步	
		try {		
			Boolean result  = futureTask.get(); //返回线程结果 后续处理
			logger.info(thisThread.getName()+"------------"+result);
			if(result) {
//				thisThread.interrupt();//停止当前线程
			}
		} catch (InterruptedException e) {
			futureTask.cancel(true);
		} catch (ExecutionException e) {
			futureTask.cancel(true);
		} catch (Exception e) {
			futureTask.cancel(true);
			// 超时后，进行相应处理
		} finally {
//			System.out.println("task@" + i + ":result=" + result);
		}

	}
}
